//
//  UTAttributedString.swift
//  iPhoneTests
//
//  Created by Toj on 3/28/23.
//

import XCTest

class UTAttributedString: XCTestCase {
    
    private let text = "Link your StarTimes decoder account to enjoy online self-service and watch VIP channels for free in App. Learn more here."
    
    private var attributedText: NSMutableAttributedString {
        let style = NSMutableParagraphStyle()
        style.lineBreakMode = .byTruncatingTail
        style.alignment = .left
        
        let highText = text.replacingOccurrences(of: ".", with: "").split(separator: Character.init(" ")).last ?? ""
        
        let font = UIFont.systemFont(ofSize: 14.f_scale)
        let (attributedText, _) = text.to_attributedString(font: font, color: "#999999".to_color, paragraph: style, highlight: String(highText), hcolor: "#0087EB".to_color, paragraph: style, hlineStyle: .underline)
        return attributedText
    }

    override func setUpWithError() throws {
        print("-->UTests: func: " + #function)
    }

    override func tearDownWithError() throws {
        print("-->UTests: func: " + #function)
    }

    func testExample() throws {
        print("-->UTests: start \n")
        
        // testAttributedStringSize()
        // testAttributedStringOneWidth()
        // testAttributedStringOneWidth1()
        // testAttributedStringOneHeight()
        // testAttributedStringHeight()
        
        print("-->UTests: end \n")
    }

    func testPerformanceExample() throws {
        print("-->UTests: func: " + #function)
        self.measure {
            print("-->UTests: func: " + #function)
        }
    }
}

extension UTAttributedString {
    
    func testAttributedStringSize() {
        let width = CGFloat.sr_width - 32.ui_scale // 16 * 2
        let maxSize = CGSize(width: width, height: 1000)
        
        CM.absoluteTimes {
            let size = attributedText.st_stringSize(maxSize: maxSize)
            print("-->UTests: testAttributedStringSize: \(size)")
        }
        CM.absoluteTimes {
            let size2 = attributedText.cm_stringSize(maxSize: maxSize)
            print("-->UTests: testAttributedStringSize: \(size2)")
        }
    }
    
    func testAttributedStringOneWidth() {
        CM.absoluteTimes {
            let width = attributedText.st_stringOneLineWidth
            print("-->UTests: testAttributedStringOneWidth: \(width)")
        }
        CM.absoluteTimes {
            let width = attributedText.cm_stringOneLineWidth
            print("-->UTests: testAttributedStringOneWidth: \(width)")
        }
    }
    
    func testAttributedStringOneWidth1() {
        CM.absoluteTimes {
            let width = attributedText.st_stringOneLineWidth1
            print("-->UTests: testAttributedStringOneWidth1: \(width)")
        }
        CM.absoluteTimes {
            let width = attributedText.cm_stringOneLineWidth1
            print("-->UTests: testAttributedStringOneWidth1: \(width)")
        }
    }
    
    func testAttributedStringOneHeight() {
        CM.absoluteTimes {
            let height = attributedText.st_stringOneLineHeight
            print("-->UTests: testAttributedStringOneHeight: \(height)")
        }
        CM.absoluteTimes {
            let height = attributedText.cm_stringOneLineHeight
            print("-->UTests: testAttributedStringOneHeight: \(height)")
        }
    }
    
    func testAttributedStringHeight() {
        let width = CGFloat.sr_width - 32.ui_scale // 16 * 2
        CM.absoluteTimes {
            let height = attributedText.st_stringHeight(maxWidth: width)
            print("-->UTests: testAttributedStringHeight: \(height)")
        }
        CM.absoluteTimes {
            let height = attributedText.cm_stringHeight(maxWidth: width)
            print("-->UTests: testAttributedStringHeight: \(height)")
        }
    }
    
    func testAttributedStringWidth() {
        let maxHeight = 200.f_scale
        CM.absoluteTimes {
            let width = attributedText.st_stringWidth(maxHeight: maxHeight)
            print("-->UTests: testAttributedStringWidth: \(width)")
        }
        CM.absoluteTimes {
            let width = attributedText.cm_stringWidth(maxHeight: maxHeight)
            print("-->UTests: testAttributedStringWidth: \(width)")
        }
    }
}
